{extend name="layout:base" /}

{block name="main"}
{:import_addon_public('css/vs.min.css')}
{:import_addon_public('js/highlight.min.js')}
<script>
    hljs.highlightAll();
    var isHighlightingEnabled = true;
</script>

<div class="row">
    <div class="col-12">
        <p class="break-word"><b>{:lng($view_title)} "{:fm_enc(fm_convert_win($file))}"</b></p>
        <p class="break-word">
            <?php $display_path = fm_get_display_path($file_path); ?>
            <strong>{$display_path['label']}:</strong> {$display_path['path']}<br>
            <strong>{:lng('File size')}:</strong> <?php echo ($filesize_raw <= 1000) ? "$filesize_raw bytes" : $filesize ; ?><br>
            <strong>MIME-type:</strong> {$mime_type}<br>
            <?php
                // ZIP info
                if (($is_zip || $is_gzip) && $filenames !== false) {
                    $total_files = 0;
                    $total_comp = 0;
                    $total_uncomp = 0;
                    foreach ($filenames as $fn) {
                        if (!$fn['folder']) {
                            $total_files++;
                        }
                        $total_comp += $fn['compressed_size'];
                        $total_uncomp += $fn['filesize'];
                    }
                    ?>
            <?php echo lng('Files in archive') ?>: <?php echo $total_files ?><br>
            <?php echo lng('Total size') ?>: <?php echo fm_get_filesize($total_uncomp) ?><br>
            <?php echo lng('Size in archive') ?>: <?php echo fm_get_filesize($total_comp) ?><br>
            <?php echo lng('Compression') ?>: <?php echo round(($total_comp / max($total_uncomp, 1)) * 100) ?>%<br>
            <?php
                }
                // Image info
                if ($is_image) {
                    $image_size = getimagesize($file_path);
                    echo lng('Image sizes').': ' . (isset($image_size[0]) ? $image_size[0] : '0') . ' x ' . (isset($image_size[1]) ? $image_size[1] : '0') . '<br>';
            }
            // Text info
            if ($is_text) {
            $is_utf8 = fm_is_utf8($content);
            if (function_exists('iconv')) {
            if (!$is_utf8) {
            $content = iconv(FM_ICONV_INPUT_ENC, 'UTF-8//IGNORE', $content);
            }
            }
            echo '<strong>'.lng('Charset').':</strong> ' . ($is_utf8 ? 'utf-8' : '8 bit') . '<br>';
            }
            ?>
        </p>
        <div class="d-flex align-items-center mb-3">
            <form method="post" class="d-inline ms-2" action="{:addon_url('admin/index/download', ['p' => FM_PATH, 'dl' => $file])}">
                {:token_field()}
                <button type="submit" class="btn btn-link text-decoration-none fw-bold p-0"><i class="fa fa-cloud-download"></i> <?php echo lng('Download') ?></button> &nbsp;
            </form>
            <!--<b class="ms-2"><a href="{:fm_enc($file_url)}" target="_blank"><i class="fa fa-external-link-square"></i> {:lng('Open')}</a></b>-->
            <?php
                // ZIP actions
                if (!FM_READONLY && ($is_zip || $is_gzip) && $filenames !== false) {
                    $zip_name = pathinfo($file_path, PATHINFO_FILENAME);
                    ?>
            <form method="post" class="d-inline ms-2" action="{:addon_url('admin/index/unpack', ['p' => FM_PATH])}">
                {:token_field()}
                <input type="hidden" name="unzip" value="{:urlencode($file)}">
                <button type="submit" class="btn btn-link text-decoration-none fw-bold p-0" style="font-size: 14px;"><i class="fa fa-check-circle"></i> {:lng('UnZip')}</button>
            </form>&nbsp;
            <form method="post" class="d-inline ms-2" action="{:addon_url('admin/index/unpack', ['p' => FM_PATH])}">
                {:token_field()}
                <input type="hidden" name="unzip" value="{:urlencode($file)}">
                <input type="hidden" name="tofolder" value="1">
                <button type="submit" class="btn btn-link text-decoration-none fw-bold p-0" style="font-size: 14px;" title="UnZip to {:fm_enc($zip_name)}"><i class="fa fa-check-circle"></i> {:lng('UnZipToFolder')}</button>
            </form>&nbsp;
            <?php
                }
                if ($is_text && !FM_READONLY) {
                    ?>
            <b class="ms-2"><a href="{:addon_url('admin/index/edit', ['p' => FM_PATH, 'edit' => $file])}" class="edit-file"><i class="fa fa-pencil-square"></i> {:lng('Edit')}
            </a></b> &nbsp;
            <b class="ms-2"><a href="{:addon_url('admin/index/edit', ['p' => FM_PATH, 'edit' => $file, 'env' => 'ace'])}"
                               class="edit-file"><i class="fa fa-pencil-square-o"></i> {:lng('AdvancedEditor')}
            </a></b> &nbsp;
            <?php } ?>
            <b class="ms-2"><a class="text-dark" href="{:addon_url('admin/index/index', ['p' => FM_PATH])}"><i class="fa fa-chevron-circle-left go-back text-dark"></i> {:lng('Back')}</a></b>
        </div>
        <?php
            if($is_onlineViewer) {
                if($online_viewer == 'google') {
                    echo '<iframe src="https://docs.google.com/viewer?embedded=true&hl=en&url=' . (request()->domain().addon_url('admin/index/preview', ['path' => $file_path, 'type' => $mime_type])) . '" frameborder="no" style="width:100%;min-height:460px"></iframe>';
                } else if($online_viewer == 'microsoft') {
                    echo '<iframe src="https://view.officeapps.live.com/op/embed.aspx?src=' . (request()->domain().addon_url('admin/index/preview', ['path' => $file_path, 'type' => $mime_type])) . '" frameborder="no" style="width:100%;min-height:460px"></iframe>';
                }
            } elseif ($is_zip) {
                // ZIP content
                if ($filenames !== false) {
                    echo '<code class="maxheight">';
                    foreach ($filenames as $fn) {
                        if ($fn['folder']) {
                            echo '<b>' . fm_enc($fn['name']) . '</b><br>';
                        } else {
                            echo $fn['name'] . ' (' . fm_get_filesize($fn['filesize']) . ')<br>';
                        }
                    }
                    echo '</code>';
                } else {
                    echo '<p>'.lng('Error while fetching archive info').'</p>';
                }
            } elseif ($is_image) {
                // Image content
                if (in_array($ext, array('gif', 'jpg', 'jpeg', 'png', 'bmp', 'ico', 'svg', 'webp', 'avif'))) {
                    echo '<p><input type="checkbox" id="preview-img-zoomCheck"><label for="preview-img-zoomCheck"><img src="' . addon_url('admin/index/preview', ['path' => $file_path, 'type' => $mime_type]) . '" alt="image" class="preview-img"></label></p>';
                }
            } elseif ($is_audio) {
                // Audio content
                echo '<p><audio src="' . addon_url('admin/index/preview', ['path' => $file_path, 'type' => $mime_type]) . '" controls preload="metadata"></audio></p>';
            } elseif ($is_video) {
                // Video content
                echo '<div class=""><video src="' . addon_url('admin/index/preview', ['path' => $file_path, 'type' => $mime_type]) . '" width="640" height="360" controls preload="metadata"></video></div>';
            } elseif ($is_text) {
                if (FM_USE_HIGHLIGHTJS) {
                    // highlight
                    $hljs_classes = array(
                        'shtml' => 'xml',
                        'htaccess' => 'apache',
                        'phtml' => 'php',
                        'lock' => 'json',
                        'svg' => 'xml',
                    );
                    $hljs_class = isset($hljs_classes[$ext]) ? 'lang-' . $hljs_classes[$ext] : 'lang-' . $ext;
                    if (empty($ext) || in_array(strtolower($file), fm_get_text_names()) || preg_match('#\.min\.(css|js)$#i', $file)) {
                        $hljs_class = 'nohighlight';
                    }
                    $content = '<pre class="with-hljs"><code class="' . $hljs_class . '">' . fm_enc($content) . '</code></pre>';
                } else if (in_array($ext, array('php', 'php4', 'php5', 'phtml', 'phps'))) {
                    // php highlight
                    $content = highlight_string($content, true);
                } else {
                    $content = '<pre>' . fm_enc($content) . '</pre>';
                }
                //dump($content);exit;
                echo $content;
            }
        ?>
    </div>
</div>
{/block}